package it.yuzuojian.com.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import it.yuzuojian.com.javaBean.Article;
import it.yuzuojian.com.mapper.ArticleMapper;
import it.yuzuojian.com.service.ArticleService;
import it.yuzuojian.com.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RedisTemplate myRedisTemplate;
    @Override
    public List<Article> findAll(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleMapper.findAll();
//        Set<String> keys = myRedisTemplate.keys("*");
//        myRedisTemplate.delete(keys);
        for (Article article : articles) {
            String key="articleid"+article.getId();
            if(redisUtil.hasKey(key)){
                article.setViewCount((Integer) redisUtil.get(key));
            }else {
                redisUtil.set(key,article.getViewCount());
            }
        }
        return articles;
    }

    @Override
    public List<Article> findAll() {
        List<Article> articles = articleMapper.findAll();
        for (Article article : articles) {
            String key="articleid"+article.getId();
            if(redisUtil.hasKey(key)){
                article.setViewCount((Integer) redisUtil.get(key));
            }else {
                redisUtil.set(key,article.getViewCount());
            }
        }
        return  articles;
    }

    @Override
    public List<Article> searchArticle(int page, int pageSize, String typeName, String title, Boolean recommend) {
        PageHelper.startPage(page,pageSize);
        return articleMapper.searchArticle(typeName,title,recommend);
    }

    @Override
    public void deleteById(Integer id) {
        articleMapper.deleteById(id);
    }

    @Override
    public void add(Article article) {
        articleMapper.add(article);
    }

    @Override
    public Integer findNextId() {
        Integer maxId = articleMapper.findMaxId();
        if(maxId==null || maxId==0){
            maxId=0;
        }
        return maxId+1;
    }

    @Override
    public Article findById(Integer id) {
        return articleMapper.findById(id);
    }

    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }

    @Override
    public Integer findCounts() {
        return articleMapper.findCounts();
    }

    @Override
    public List<Article> findRecommendNewArticle() {
        return articleMapper.findRecommendNewArticle();
    }

    @Override
    public List<Article> findByTypeName(int page,int pageSize,String typeName) {
        PageHelper.startPage(page,pageSize);
        return articleMapper.findByTypeName(typeName);
    }

    @Override
    public List<Article> findByTagId(int page,int pageSize,Integer tagId) {
        PageHelper.startPage(page,pageSize);
        return articleMapper.findByTagId(tagId);
    }

    @Override
    public void addViewCount(Integer id) {
        String key="articleid"+id;
        redisUtil.incr(key,1);
    }
    @Override
    public void addViewCount(Integer id,Integer viewCount){
        articleMapper.updateViewCount(id,viewCount);
    }

    @Override
    public List<Article> search(int page,int pageSize,String query) {
        PageHelper.startPage(page,pageSize);
        return articleMapper.search(query);
    }

/*    @Override
    public Integer searchCounts(String query) {
        return articleMapper.searchCounts(query);
    }*/
}
